<?php
/*
curl用于向网页抓取数据，可用于模拟登录, IP代理, 传输cookie 和  post数据 

使用之前请检查php是否开启curl

http://www.jb51.net/article/25193.htm
http://blog.51yip.com/php/1039.html
*/


// 远程获取方法
function curl_data($url='',$data=array(),$cookie=''){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	if(!empty($data)){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	}
	if(!empty($cookie)) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result= curl_exec($ch);
	curl_close ($ch);
	return $result;
}
// 构造cookie
$aCookie = '';
foreach($_COOKIE as $keyC => $vC){
	$aCookie[] = $keyC.'='.urlencode($vC);
}
// 获取头部文件
$cookie = implode('; ', $aCookie);
$sHeader = curl_data('http://www.che1an.com/index.php?m=carMall&a=header',$data=array(),$cookie);

// 替换 title keywords description
$site_title = (isset($SEO['title']) && !empty($SEO['title'])) ? $SEO['title'] : '';
$site_title .= $SEO['site_title'];
$keyword = $SEO['keyword'];
$description = $SEO['description'];

// 批量替换
$patterns = array(
		"'href=\"\/'",
		"'class=\"cur\">商城首页'",
		"'>行业资讯'",
		"'<title>.*?</title>'",
		"'<meta name=\"keywords\".*?\">'",
		"'<meta name=\"description\".*?\">'",
);
$replacements = array(
		"href=\"http://www.che1an.com/",
		"class=\"\">商城首页",
		" class=\"cur\" >行业资讯",
		"<title>".$site_title."</title>",
		"<meta name=\"keywords\" content=\"".$keyword."\">",
		"<meta name=\"description\" content=\"".$description."\">",
);
$sHeader = preg_replace($patterns, $replacements, $sHeader);
// 输出
echo $sHeader;